package com.supreme.studyserver.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.supreme.studyserver.entity.MysqlTest;
import com.supreme.studyserver.mapper.MysqlTestMapper;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class MysqlTestService {

    @Resource
    private MysqlTestMapper mysqlTestMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    @Resource
    private SqlSessionTemplate sqlSessionTemplate;

    public List<MysqlTest> testList(LambdaQueryWrapper<MysqlTest> queryWrapper) {
        return mysqlTestMapper.selectList(Wrappers.emptyWrapper());
    }

    public List<MysqlTest> selectTestList(MysqlTest test) {
        return mysqlTestMapper.selectTestList(test);
    }

    public List<MysqlTest> selectTestList2(Map<String, String> test) {
        JSONObject jsonObject = new JSONObject();
        String firstName = test.get("firstName");
        if (Objects.nonNull(firstName)) {
            jsonObject.put("firstName", firstName);
        }
        String lastName = test.get("lastName");
        if (Objects.nonNull(lastName)) {
            jsonObject.put("lastName", lastName);
        }
        return mysqlTestMapper.selectTestList2(jsonObject);
    }

    public void showTestList() {
        List<MysqlTest> testList = mysqlTestMapper.selectList(Wrappers.emptyWrapper());
        testList.forEach(System.out::println);
    }

    public int insertTest(MysqlTest test) {
        return mysqlTestMapper.insertTest(test);
    }

    public int insertRandomTest() {
        MysqlTest mysqlTest = new MysqlTest();
        String firstName = RandomStringUtils.randomAlphabetic(4);
        mysqlTest.setFirstName(firstName);
        String lastName = RandomStringUtils.randomAlphabetic(4);
        mysqlTest.setLastName(lastName);
        return mysqlTestMapper.insertTest(mysqlTest);
    }

    public int batchInsertLoopInsert(Long insertCount) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        int result = 0;
        for (int i = 0; i < insertCount; i++) {
            MysqlTest mysqlTest = new MysqlTest();
            mysqlTest.setFirstName("firstName" + i);
            mysqlTest.setLastName("lastName" + i);
            result += mysqlTestMapper.insert(mysqlTest);
        }

        stopWatch.stop();
        double totalTimeSeconds = stopWatch.getTotalTimeSeconds();
        System.out.println("totalTimeSeconds = " + totalTimeSeconds);
        return result;
    }

    public int batchInsertFromForEach(Long insertCount) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        List<MysqlTest> testList = new ArrayList<>();
        for (int i = 0; i < insertCount; i++) {
            MysqlTest mysqlTest = new MysqlTest();
            mysqlTest.setFirstName("firstName" + i);
            mysqlTest.setLastName("lastName" + i);
            testList.add(mysqlTest);
        }
        int result = mysqlTestMapper.batchInsertTest(testList);

        stopWatch.stop();
        double totalTimeSeconds = stopWatch.getTotalTimeSeconds();
        System.out.println("totalTimeSeconds = " + totalTimeSeconds);
        return result;
    }

    public int batchInsertFromSqlSession(Long insertCount) {
        StopWatch stopWatch = new StopWatch();
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        MysqlTestMapper sessionMapper = sqlSession.getMapper(MysqlTestMapper.class);
        try {
            stopWatch.start();
            for (int i = 0; i < insertCount; i++) {
                MysqlTest mysqlTest = new MysqlTest();
                mysqlTest.setFirstName("firstName" + i);
                mysqlTest.setLastName("lastName" + i);
                sessionMapper.insert(mysqlTest);
            }
            sqlSession.commit();
            sqlSession.clearCache();
        } catch (Exception e) {
            sqlSession.rollback();
        } finally {
            sqlSession.close();

            stopWatch.stop();
            double totalTimeSeconds = stopWatch.getTotalTimeSeconds();
            System.out.println("totalTimeSeconds = " + totalTimeSeconds);
        }
        return 0;
    }
}
